package personal.leil.learning.undigraph;

/**
 * 无向图的边
 * @author Leil
 * @version 1.0
 * @since 2024/1/23
 */
public class EdgeUndirected implements Comparable<EdgeUndirected> {

    /**
     * 边连接的两个顶点
     */
    public Vertex[] vertices;

    /**
     * 边的权重
     */
    public int weight;

    public EdgeUndirected(Vertex one, Vertex two, int weight) {
        vertices = new Vertex[]{one, two};
        one.addEdge(this);
        two.addEdge(this);
        this.weight = weight;
    }

    public Vertex findAnother(Vertex vertex) {
        return vertex == vertices[0] ? vertices[1] : vertices[0];
    }

    @Override
    public int compareTo(EdgeUndirected o) {
        return this.weight - o.weight;
    }

    @Override
    public String toString() {
        return vertices[0] + "===(" + weight + ")" + vertices[1];
    }
}
